#include "MenuSystem.h"
#include "IrControl.h"
#include "Menu/Tool/frame.h"
#include "BF533/spi.h"


#define ITEM_BAS_BAS_DOWN		(0)
#define ITEM_BAS_BAS_UP	(1)
#define ITEM_BAS_TRE_DOWN		(2)
#define ITEM_BAS_TRE_UP	(3)
#define ITEM_BAS_END		(4)

#define AUDIO_BAS_BUTTON_QTY	(5)
#define AUDIO_BAR_DISTANCE  28

TRect		AudioBASTouchRect[AUDIO_BAS_BUTTON_QTY];
TRect		AudioBASInfoPos= {28,140,168,183};
TRect		AudioTREInfoPos= {28,265,169,306};
TPosCoor		AudioBASBarPos= {284,134};
TPosCoor		AudioTREBarPos= {284,254};
TPosCoor	AudioBASItemPos[AUDIO_BAS_BUTTON_QTY] = { {172,120}, {586,120}, {172,240}, {586,240}, {576,376} };
char*		AudioBASInfo = { "END"};
int		AudioBASFocus = -1;
TFrame		AudioBASFrame;
char		AudioBASInfoStr[40];
static int		BASValue = 0;
static int		TREValue = 0;
//unsigned char BASTRE_data [5] = {0xe0,0x03,0x06,0x09,0xF2};
extern  unsigned char send_data_buf[5];
extern	unsigned char read_info_data[10];
extern  TRect	  MuteInfoPos;
extern int volume_count ;
extern int volume_temp;
extern unsigned char mute_status;
extern unsigned char	VOLvalue ;
extern struct _VolumePos VolumePos;
extern TRect	BarInfoRect;
extern unsigned char rece_handle;	
extern unsigned char dtpresent_flag;
extern pthread_cond_t dt_cond;



int  BASInfoUpdata (void)
{
	int value;
	
		value = read_info_data[4] - 6;
	switch (read_info_data[3])
		{
			case	ID_SCREEN_INFO_BAS:
				BASValue = value;
				break;
			case  ID_SCREEN_INFO_TRE:
				TREValue = value;
				break;
			}
			return 0;
}

void AudioBASDrawBar( TCanvas *pCanvas, int idx)
{
	char FileName[20];
	int GetJpegIdx; 
	TPosCoor	 pos;
	int value,i;
	
	if (idx < ITEM_BAS_TRE_UP) 
		{
		pos.x = AudioBASBarPos.x;
		pos.y = AudioBASBarPos.y;
		value = BASValue +5;
		}
	else
		{
		pos.x = AudioTREBarPos.x;
		pos.y = AudioTREBarPos.y;
		value = TREValue +5;
		}


	for ( i=0;  i<11; i++)
		{	
		if ( i==value)
			GetJpegIdx = IMG_AUDIO_BAS_HLBAR ;
	    	else
			GetJpegIdx = IMG_AUDIO_BAS_DEFBAR;
			sprintf( FileName, "res_jpeg/%d/%d.jpg", AUDIO_BAS_FOLDER, GetJpegIdx);
			DrawJpeg(pCanvas, pos.x+AUDIO_BAR_DISTANCE*i,  pos.y, FileName);	
		//printf ("draw bar: pos.x:%d,pos.y:%d,GetJpegIdx :%d,value is %d\n",pos.x+AUDIO_BAR_DISTANCE*i,pos.y,GetJpegIdx,value);
		}
	GetJpegIdx = IMG_AUDIO_BAS_MIDBAR ;
	sprintf( FileName, "res_jpeg/%d/%d.jpg", AUDIO_BAS_FOLDER, GetJpegIdx);
	DrawJpeg(pCanvas, pos.x+AUDIO_BAR_DISTANCE*5,  pos.y, FileName);	

	//printf ("in AudioBASDrawBar \n");
	
}

void AudioBASDrawInfo( TCanvas *pCanvas, int idx)
{
	TJpeg pJpeg;
	TRect rect; 
	char FileName[20];
	int GetJpegIdx; 

	g_SetTextFormat( FONT33, TEXT_NORMAL, RGB_WHITE, 0, 0 );
	switch(idx)
	{
		case ITEM_BAS_BAS_UP:
		case ITEM_BAS_BAS_DOWN:			
			GetJpegIdx = IMG_AUDIO_BAS_BASCOVER;
			sprintf( FileName, "res_jpeg/%d/%d.jpg", AUDIO_BAS_FOLDER, GetJpegIdx);	
			pJpeg = GetJpegPtr(FileName);
			rect.left = AudioBASInfoPos.left;
			rect.top = AudioBASInfoPos.top;				
			sprintf(AudioBASInfoStr, "BAS %d", BASValue);	
			AudioBASDrawBar(pCanvas,idx);
			break;
			
		case ITEM_BAS_TRE_UP:
		case ITEM_BAS_TRE_DOWN:
			GetJpegIdx = IMG_AUDIO_BAS_TRECOVER;
			sprintf( FileName, "res_jpeg/%d/%d.jpg", AUDIO_BAS_FOLDER, GetJpegIdx);	
			pJpeg = GetJpegPtr(FileName);
			rect.left = AudioTREInfoPos.left;
			rect.top = AudioTREInfoPos.top;			
			sprintf(AudioBASInfoStr, "TRE %d", TREValue);
			AudioBASDrawBar(pCanvas,idx);
			break;	
	}
	
	rect.right = rect.left + pJpeg.Width;	
	rect.bottom = rect.top + pJpeg.Height;
	DrawJpeg(pCanvas, rect.left, rect.top, FileName);		
	g_Puts( pCanvas, FONT33, rect, TEXT_ALIGN_LEFT, AudioBASInfoStr );
	

}


void AudioBASDrawBackgroup(TCanvas *pCanvas)
{	
	

	//GetJpegIdx = IMG_AUDIO_EQ_OFF;

	//sprintf( FileName, "res_jpeg/%d/%d.jpg", AUDIO_BAS_FOLDER, GetJpegIdx);	
	//DrawJpeg(pCanvas, 0, 0, "/res_jpeg/audio_eq.jpg" );
	DrawJpeg(pCanvas, 0, 0, "/res_jpeg/bkg.jpg" );
	DrawJpeg(pCanvas, 0, 8, "/res_jpeg/bar.jpg" );
	g_Puts( pCanvas, FONT33, BarInfoRect, TEXT_ALIGN_CENTER, "BAS/TRE SET" );

}



void AudioBASDrawVolume( TCanvas *pCanvas,int flag)
{
	int GetJpegIdx;
	TRect rect;
	TJpeg pJpeg;
	char FileName[20];
	int x,y;
	int i;
	switch (flag)
		{							
			case 1://disp volume all
				GetJpegIdx = IMG_VOLUME_BARCOVER;
				sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_OTHER_FOLDER, GetJpegIdx);	
				DrawJpeg(pCanvas, VolumePos.Volume_all.x, VolumePos.Volume_all.y, FileName);	
				if (mute_status == 1)
					{
						GetJpegIdx = IMG_VOLUME_MUTE ;
						sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_OTHER_FOLDER, GetJpegIdx);
						DrawJpeg(pCanvas, VolumePos.Volume_speaker.x, VolumePos.Volume_speaker.y, FileName);	
					}	
				else
						mute_status = 2;
				
				for (i = 0; i<VOLvalue; i++)
					{
					if (i%2)
						{
						x = VolumePos.Volume_bar_start.x+VolumePos.Distance*(i/2) + VolumePos.Barwidth;
						GetJpegIdx = IMG_VOLUME_RIGHTFOCUS;
						sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_OTHER_FOLDER, GetJpegIdx);
						}
					else
						{
						x = VolumePos.Volume_bar_start.x+VolumePos.Distance*(i/2) ;
						GetJpegIdx = IMG_VOLUME_LEFTFOCUS;
						sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_OTHER_FOLDER, GetJpegIdx);
						}
					y = VolumePos.Volume_bar_start.y;					
					DrawJpeg(pCanvas,x, y, FileName);						
					}
			break;			
			
			case 2://draw bar
			
			break;
			
			case 3://undisp
				DrawJpeg(pCanvas, 0, 8, "/res_jpeg/bar.jpg" );				
				g_Puts( pCanvas, FONT33, BarInfoRect, TEXT_ALIGN_CENTER, "BAS/TRE SET" );
			break;
		}
	
	pJpeg = GetJpegPtr("/res_jpeg/bar.jpg" );
	rect.left = BARPOS_X;
	rect.right = rect.left + pJpeg.Width;
	rect.top = BARPOS_Y;
	rect.bottom = rect.top + pJpeg.Height;	
	dt_Present_part(rect);	
	volume_count = 0;
	volume_temp = 0;

}


static void VolumeCountHandle(void)
{
	volume_count ++;
		if (volume_count >0xFF00)
			{
				volume_temp ++;
				volume_count = 0;
			}
		if (volume_temp > 10)
			{
				 AudioBASDrawVolume(AudioBASFrame.pCanvas,3);					
				volume_count = 0;	
				volume_temp = 0;	
				mute_status =0;
			}
}


void AudioBASDrawItem( TCanvas *pCanvas, int idx, BOOL bfocus )
{
	TJpeg pJpeg;
	TPosCoor pos;
	int tmpid;
	char FileName[20];
	int GetJpegIdx;
	
	if( idx < 0 || idx >= AUDIO_BAS_BUTTON_QTY )
		return;

        if(idx == 4)
        	{
        	tmpid = 2;
        	}
        else
        	tmpid = idx % 2;

	if( bfocus ){
		GetJpegIdx = IMG_AUDIO_BAS_ITEM_FOCUS_LEFT+tmpid ;
	}else{
		GetJpegIdx = IMG_AUDIO_BAS_ITEM_LEFT+tmpid ;
	}
	pos = AudioBASItemPos[idx];
	sprintf( FileName, "res_jpeg/%d/%d.jpg", AUDIO_BAS_FOLDER, GetJpegIdx);	
	pJpeg = GetJpegPtr(FileName);
	DrawJpeg(pCanvas, pos.x, pos.y, FileName);

	AudioBASTouchRect[idx].left = pos.x;
	AudioBASTouchRect[idx].right = pos.x + pJpeg.Width;
	AudioBASTouchRect[idx].top = pos.y;
	AudioBASTouchRect[idx].bottom = pos.y + pJpeg.Height;	
	//printf ("tmpid is %d,file:%s\n",tmpid, FileName);
	if (idx == ITEM_BAS_END)
	g_Puts( pCanvas, FONT33, AudioBASTouchRect[idx], TEXT_ALIGN_CENTER, AudioBASInfo );	

}

void AudioBASDraw( BOOL bDrawAll )
{
	int i;
	if( bDrawAll )
		AudioBASDrawBackgroup(AudioBASFrame.pCanvas);
	for( i = 0; i < AUDIO_BAS_BUTTON_QTY; i++ )
		AudioBASDrawItem( AudioBASFrame.pCanvas, i, FALSE );
	AudioBASDrawInfo( AudioBASFrame.pCanvas, ITEM_BAS_BAS_UP);
	AudioBASDrawInfo( AudioBASFrame.pCanvas, ITEM_BAS_TRE_UP);
	if (mute_status == 1)
		{
			 AudioBASDrawVolume(AudioBASFrame.pCanvas,1);		
		}
	dtpresent_flag++;		
	pthread_cond_signal(&dt_cond);
	//dt_Present();
}

int AudioBASGetTouchID( TPosCoor pos )
{
	int i;
	for( i = 0; i < AUDIO_BAS_BUTTON_QTY; i++ )
		if( ut_PtInRect( pos, AudioBASTouchRect[i] ) )
			return i;
	return -1;
}

void AudioBASBtnProc(int idx)
{

	AudioBASDrawItem( AudioBASFrame.pCanvas, idx, FALSE );	
	//AudioBASDrawInfo( AudioBASFrame.pCanvas, idx);////20080718 masked
	//dt_Present();
	dtpresent_flag++;		
	pthread_cond_signal(&dt_cond);
	/*dt_Present_part(AudioBASTouchRect [idx]);	
	dt_Present_part(AudioBASInfoPos);
	dt_Present_part(AudioTREInfoPos);*/
	
}

BOOL AudioBASBtnEnter(int idx)
{
	//AudioBASDrawItem( AudioBASFrame.pCanvas, idx, FALSE );//////////////20080221 mask it
	switch(idx)
	{
		case ITEM_BAS_BAS_UP:
		case ITEM_BAS_BAS_DOWN:
		case ITEM_BAS_TRE_UP:
		case ITEM_BAS_TRE_DOWN:
			AudioBASBtnProc(idx);
			break;

		case ITEM_BAS_END:
			MenuAudioInit();
			break;
	}
	LOG( "btn = %d\n", idx );	

	return TRUE;
}
void AudioBASInit()
{
	ir_SetCurrentID( IR_AUDIO_BAS_INIT );

	frm_Create( &AudioBASFrame, FRM_NO_TITLE|FRM_NO_TASK, "" );
	AudioBASDraw( TRUE );
}


/*****************************
function:message_dispatch

 return:
  0: successful
  1: need other menu handle
  -1:failure
*****************************/
static int message_dispatch (void)
{
	int rc = 1;
	
	switch (read_info_data[2])
	{
					case	ID_POWER:						
						break;
						
					case	ID_DISPLAY_SCREEN:
							switch (read_info_data[3])
								{
									case 	ID_SCREEN_MENU:
											MenuMainInit();
										break;		
									case 	ID_SCREEN_RADIO:
											MenuRadioInit();
										break;									
									case	ID_SCREEN_CDLOAD:											
									case	ID_SCREEN_CDDISP:
									case	ID_SCREEN_MP3DISP:
									case	ID_SCREEN_NODISC:
									case	ID_SCREEN_DISCERR:
									case	ID_SCREEN_EJECT:
											MenuCdInit();
											rece_handle = 0;
										break;
									case	ID_SCREEN_MEDIEA:
											MenuMediaInit();
										break;
									
									case 	ID_SCREEN_NAVI:
										MenuNaviInit();
										break;	
										
									case	 	ID_SCREEN_AUDIODISP:
											MenuAudioInit();
											break;	
									case		ID_SCREEN_VIDEODISP:
										MenuVideoInit();
										break;	
									case		ID_SCREEN_EQDISP:
											AudioEQInit();
										break;	
									case	 	ID_SCREEN_BASTRA:
											AudioBASInit();
										break;	
									case		ID_SCREEN_FADBAL:	
											AudioFADInit();							
										break;	
									}
									rc = 0;//if handled, clear this flag.	
							read_info_data[0] = 0;
																									
						break;
						
					case	ID_KEY_STATUS:						
						break;
						
					case	ID_SCREEN_INFO:
						if (read_info_data[3] == ID_SCREEN_INFO_VOLUME)
								{
										//VolumeInit();
									break;
								}	
						switch (read_info_data[3])
							{
							case ID_SCREEN_INFO_VOLUME:								
								 AudioBASDrawVolume(AudioBASFrame.pCanvas,1);	
								break;
							case  ID_SCREEN_INFO_BAS:
							case ID_SCREEN_INFO_TRE:
								AudioBASDrawInfo( AudioBASFrame.pCanvas, ITEM_BAS_BAS_UP);
								AudioBASDrawInfo( AudioBASFrame.pCanvas, ITEM_BAS_TRE_UP);
								dtpresent_flag++;		
								pthread_cond_signal(&dt_cond);
								//AudioBASDrawBar(AudioBASFrame.pCanvas,1);
								break;								
							}
						break;
						
					case	ID_KEYPAD:	
							switch (read_info_data[3])
							{
									case 	ID_MUTE:	
										AudioBASDrawVolume(AudioBASFrame.pCanvas,1);
										break;
									case	ID_DMUTE:	
										AudioBASDrawVolume(AudioBASFrame.pCanvas,3);
										break;						
									case 	ID_SEEKUP:
									case 	ID_SEEKDN:
										break;
									case 	ID_DEST:
									case 	ID_NAVI:
										MenuNaviInit();
										break;
									case 	ID_FMAM:
										//band command;
										break;
									case 	ID_CD:
										MenuCdInit();
										break;				
									case 	ID_MEDIA:
										MenuMediaInit();
										break;
									case 	ID_MENU:
										MenuMainInit();
										break;
										
									case	ID_TVAUX:
										
										break;
							}	
							rc = 0;//if handled, clear this flag.	
							read_info_data[0] = 0;			 
						break;
						
						case 	ID_BAND_SEL:
							MenuRadioInit();
							rc = 0;//if handled, clear this flag.	
							read_info_data[0] = 0;
						break;	
						
					default:	
							rc = -1;						
						}
return 0;						
}

/*
static int send_data_out ( int index)
{
	if ((index < 0) || (index > AUDIO_BAS_BUTTON_QTY))
		return -1;
		
		switch (index)
		{
			case ITEM_BAS_BAS_UP:
				BASTRE_data [2] = 0x06;
				BASTRE_data [3] = 0x09 ;
				BASTRE_data [4] = 0xF2 ;
				break;
			case ITEM_BAS_BAS_DOWN:
				BASTRE_data [2] = 0x06;
				BASTRE_data [3] = 0x0a ;
				BASTRE_data [4] = 0xF3 ;
				break;
			case ITEM_BAS_TRE_UP:
				BASTRE_data [2] = 0x06;
				BASTRE_data [3] = 0x0b;
				BASTRE_data [4] = 0xf4;
				break;
			case ITEM_BAS_TRE_DOWN:
				BASTRE_data [2] = 0x06;
				BASTRE_data [3] = 0x0c;
				BASTRE_data [4] = 0xf5;
				break;
			case ITEM_BAS_END:
				BASTRE_data [2] = 0x07;
				BASTRE_data [3] = 0x01;
				BASTRE_data [4] = 0xeb;
				break;
		}

		if (dev_SPIWrite(BASTRE_data,5)==0)
			return 0;//hangsheng 071126	
	return -1;//add 20080708
}

*/
static int send_data ( int index)
{
	if ((index < 0) || (index > AUDIO_BAS_BUTTON_QTY))
		return -1;
		
		switch (index)
		{
			case ITEM_BAS_BAS_UP:
				send_data_buf [2] = IDDATA_AUDIO;
				send_data_buf [3] = AUDIO_BAS_UP;
				break;
			case ITEM_BAS_BAS_DOWN:
				send_data_buf [2] = IDDATA_AUDIO;
				send_data_buf [3] = AUDIO_BAS_DOWN;
				break;
			case ITEM_BAS_TRE_UP:
				send_data_buf [2] = IDDATA_AUDIO;
				send_data_buf [3] = AUDIO_TRE_UP;
				break;
			case ITEM_BAS_TRE_DOWN:
				send_data_buf [2] = IDDATA_AUDIO;
				send_data_buf [3] = AUDIO_TRE_DOWN;
				break;
			case ITEM_BAS_END:
				send_data_buf [2] = IDDATA_ESC;
				send_data_buf [3] = ESC_BUTTON;
				break;
		}


			
		send_data_buf[0] =  HEAD;		
		send_data_buf[1] =  LENGTH;		
		return ( send_data_out(send_data_buf,ALL_LENGTH) );
		
	return -1;
}


void AudioBASInput(UInt32 key)
{
			int flag;
	
		if ( key == IRC_READ_INFO)
		{
			flag =message_dispatch ();
			if (flag == -1)
				printf ("main menu handle receive data error!\n");
			if (flag == 0)
				{
					//printf ("main menu handle receive data success!\n");
					return;
				}			
		}
		
	if( key == IRC_TOUCH_DOWN || key == IRC_TOUCH_UP )
	{
		int focus;
		TPosCoor pos;
		dt_ClipTouchGetXY( &pos.x, &pos.y );
		if( key == IRC_TOUCH_DOWN )
		{
			if( LastKey ==IRC_TOUCH_UP )
			{
				if( ( AudioBASFocus = AudioBASGetTouchID( pos )) != -1 )
				{
					AudioBASDrawItem( AudioBASFrame.pCanvas, AudioBASFocus, TRUE );
					//dt_Present();
					//dtpresent_flag++;		
					//pthread_cond_signal(&dt_cond);
					dt_Present_part(AudioBASTouchRect [AudioBASFocus]);
					if ( send_data(AudioBASFocus))
					printf ("write failure !\n");						
				}
			}
		}
		else
		{
			if( ( focus = AudioBASGetTouchID( pos ) ) == AudioBASFocus )
				AudioBASBtnEnter( AudioBASFocus );
			else if( AudioBASFocus != -1 )
			{
				AudioBASDrawItem( AudioBASFrame.pCanvas, AudioBASFocus, FALSE );
				//dt_Present();
				//dtpresent_flag++;		
				//pthread_cond_signal(&dt_cond);
				dt_Present_part(AudioBASTouchRect [AudioBASFocus]);
			}
			AudioBASFocus = -1;
		}
		LastKey = key;//hangsheng add 071126
	}
	if(mute_status ==2) 
	VolumeCountHandle ();
}

